package todlu.RssReader.QQWeibo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import todlu.RssReader.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

public class OAuthWebActivity extends Activity {
	private ImageView btnBack;
	private WebView	web;
	public final static int RESULT_CODE=1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 必须在setContentView之前使用这两句
		// 设置标题栏不显示
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.oauth_web);
		
		attachViews();
		setListeners();
		
		Intent intent = this.getIntent();
		if (!intent.equals(null))
		{
			Bundle bundle = intent.getExtras();
			if (bundle != null)
			{
				if(bundle.containsKey("Url"))
				{
					String urlStr = bundle.getString("Url");
					WebSettings webSettings = web.getSettings();
					webSettings.setJavaScriptEnabled(true);
					webSettings.setSupportZoom(true);
					web.requestFocus();
					web.loadUrl(urlStr);
				}
			}
		}

	}
	

	private void attachViews()
	{
		btnBack = (ImageView)findViewById(R.id.back);
		web = (WebView)findViewById(R.id.webView1);
	}
	
	private void setListeners() {
		// TODO Auto-generated method stub
		btnBack.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
			
		});
		
		web.addJavascriptInterface(new JavaScriptInterface(), "Methods");
		WebViewClient client = new WebViewClient()
		{
			@Override
			public void onPageFinished(WebView view, String url)
			{
				Log.i("RSSReader-QWeibo", "WebView onPageFinished...");
				
				view.loadUrl("javascript:window.Methods.getHTML('<head>'+document.getElementsByTagName('body')[0].innerHTML+'</head>');");
				super.onPageFinished(view, url);
			}
		};
		web.setWebViewClient(client);
	}
	
	class JavaScriptInterface
	{
		private static final String TAG = "MainActivity";
		public void getHTML(String html)
		{
			String verifier = getVerifier(html);
			if (!TextUtil.isEmpty(verifier))
			{
				Log.i(TAG, "verifier:"+verifier);
				Intent intent=new Intent();
				intent.putExtra("verifier", verifier);
				setResult(RESULT_CODE, intent);
				finish();
			}
		}

		public String getVerifier(String html)
		{
			String ret = "";
			String regEx = "授权码：[0-9]{6}"; //匹配验证码的正则表达式
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(html);
			boolean result = m.find();
			if (result)
			{
				ret = m.group(0).substring(4);
			}
			return ret;
		}
	}

}
